<template>
  <div>
    <el-tooltip :content="tooltipValue" :disabled="topDisabled" class="item" effect="dark" placement="top-start">
      <el-select v-model="selectValue" :disabled="disabled" :multiple="multiple" :placeholder="placeholder" style="width: 100%" clearable @change="chaangeValue">
        <el-option
          v-for="item in Options"
          :key="item.id"
          :label="item.value"
          :value="item.code"
        />
      </el-select>
    </el-tooltip>
  </div>
</template>

<script>
/*
  * typeCode   需要查询的code
  *
  * value 选中的值
  *
  * */
import { findDictionaryData, dictionaryDataFind } from 'api/common'
export default {
  name: 'Index',
  // props: ['typeCode', 'value', 'placeholder', 'disabled'],
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    typeCode: {
      default: '',
      type: String
    },
    value: {
      default: '',
      type: String
    },
    placeholder: {
      default: '',
      type: String
    },
    // 是否支持多选
    multiple: {
      type: Boolean,
      default: false
    },
    isAdmin: {
      type: Boolean,
      default: true
    },
    filter: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      selectValue: '',
      Options: [],
      tooltipValue: ''
    }
  },
  computed: {
    topDisabled() {
      return true
    }
  },
  watch: {
    selectValue(newData) {
      if (newData == '') {
        this.tooltipValue = ''
        return
      }
      this.Options.forEach((value) => {
        if (value.code == newData) {
          this.tooltipValue = value.name
        }
      })
    },
    value(newData, oldData) {
      this.selectValue = this.value
    },
    filter() {
      if (this.isAdmin) {
        this.getDictionaryData()
      } else {
        this._dictionaryDataFind()
      }
    }
  },
  created() {
    if (this.isAdmin) {
      this.getDictionaryData()
    } else {
      this._dictionaryDataFind()
    }
    this.selectValue = this.value
  },
  methods: {
    chaangeValue(item) {
      this.$emit('input', item)
      this.$emit('change', item)
    },
    // 查询后台管理系统的数据字典
    getDictionaryData() {
      var self = this
      if (self.typeCode == '') return
      this.Options = []
      var tempData = {
        code: self.typeCode
      }
      findDictionaryData(tempData).then(response => {
        var dictionaryData = response.data.dictionaryData
        if (dictionaryData) {
          var children = dictionaryData.children
          children.forEach((value, index, array) => {
            if (value.isEnabled) {
              if (this.filter.indexOf(value.code) < 0) {
                this.Options.push(value)
              }
            }
          })
        } else {
          this.Options = []
        }
      })
    },

    // 查询账户数据的数据字典
    _dictionaryDataFind() {
      var self = this
      if (self.typeCode == '') return
      this.Options = []
      var tempData = {
        code: self.typeCode
      }
      dictionaryDataFind(tempData).then(response => {
        var dictionaryData = response.data.dictionaryData
        if (dictionaryData) {
          var children = dictionaryData.children
          children.forEach((value, index, array) => {
            if (value.isEnabled) {
              if (this.filter.indexOf(value.code) < 0) {
                this.Options.push(value)
              }
            }
          })
        } else {
          this.Options = []
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
